// pages/manage/manage.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      area: ['广东省', '广州市', '海珠区']
    },
    array: ['女', '男'],
    index: 0,
    // customItem: '全部'
  },
  formSubmit: function(e) {
    if (!e.detail.value.username) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return false;
    } else if (!/^1[0-9]{10}$/.test(e.detail.value.number)) {
      wx.showToast({
        title: '请填写正确的电话号码',
        icon: 'none'
      })
      return false;
    } else if (e.detail.value.age == "") {
      wx.showToast({
        title: '请填写年龄',
        icon: 'none'
      })
      return false;
    } else if (e.detail.value.wechatNumber == '') {
      wx.showToast({
        title: '请填写微信号',
        icon: 'none',
        duration: '2000'
      });
      return false
    } else if (e.detail.value.region == '') {
      wx.showToast({
        title: '请选择地址',
        icon: 'none',
        duration: '2000'
      });
      return false
    }
    let that = this;

    var a = getApp().core.getStorageSync(getApp().const.SHARE_SETTING), o = getApp().getUser();

    getApp().request({
      url: getApp().api.share.apply_manager,
      method: "POST",
      data: {
        real_name: e.detail.value.username,
        sex: (e.detail.value.gender == '男' ? 1 : 0),
        age: e.detail.value.age,
        mobile: e.detail.value.number,
        wechat_account: e.detail.value.wechatNumber,
        area: e.detail.value.area.join()
      },
      success: function (res) {

        wx.showModal({
          title: '提示',
          content: res.code.msg || res.code.rejected_reason,
          showCancel: false,
          success (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1,
              })
            } 
          }
        })
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    })
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    this.setData({
      store: getApp().core.getStorageSync(getApp().const.STORE)
    })
    getApp().request({
      url: getApp().api.share.get_manager,
      success: (res)=> {
        if (res.code == 0 && res.data != null) {
          res.data.area = res.data.area.split(',')
          this.setData({
            form: res.data
          })
          let msg = res.data.status == 0 ? '您的申请已提交，请等待审核' : '您的申请已驳回，需要再次申请请联系客服'
          wx.showModal({
            title: '提示',
            content: msg,
            showCancel: false,
            success: res=>{
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
        else if (res.code.code == -1) {
          wx.showToast({
            title: res.data.rejected_reason,
          })
        }
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  pickChange: function(e) {
    this.setData({
      'form.sex': e.detail.value
    });
  },
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
    var area = 'form.area'
    this.setData({
      [area]:e.detail.value
    })
  }
})